<template>
  <view>
    <!-- 顶部自定义导航 -->
    <TnNavbar fixed alpha customBack :placeholder="false" bg-color="transparent" :bottom-shadow="false">
      <view slot="back" class='tn-custom-nav-bar__back'>
        <text class='icon tn-icon-left'></text>
        <text class='icon tn-icon-home-capsule-fill'></text>
      </view>
      <view class="flex align-center">
        赛迷小助手呀
        <view
            style="width: 15px;height: 15px;border-radius: 100%;"
            class="flex justify-center align-center bg-blue margin-left-xs"
            @click="showInstruction = true"
        >
          ?
        </view>
      </view>
    </TnNavbar>
    <view :style="{height: height + 'px'}"></view>
    <scroll-view
        scroll-y
        :style="{height: `calc(100vh - ${height + safeBottom}px)`}"
        :scroll-into-view="topItem"
        scroll-with-animation
        @scroll="handleScroll"
    >
      <view id="top"></view>
      <view v-if="list && list.length > 0">
        <block v-for="(item, index) in list" :key="index">
          <view class="article-shadow margin">
            <view class="tn-flex" v-for="v in item.content.news_item" @click="gotoArticleDetail(v.url)">
              <view class="margin-sm padding-top-xs" style="width: 100%;">
                <view class="text-lg text-bold clamp-text-2 tn-text-justify" style="min-height: 95rpx;">
                  <text class="">{{ v.title }}</text>
                </view>
                <view class="padding-top-xs">
                  <text class="text-sm text-gray clamp-text-1">
                    {{ v.digest }}
                  </text>
                </view>
                <view class="text-sm text-blue"
                      style="padding-top: 15rpx;">
                  <text>{{ new Date(item.content.create_time * 1000).toLocaleString() }}</text>
                </view>
              </view>
              <view class="image-pic margin-sm" :style="'background-image:url(' + v.thumb_url + ')'">
                <view class="image-article">
                </view>
              </view>
            </view>
          </view>
        </block>
        <view class="tn-flex-center">
          <TnButton text @click="loadMore" debounce :disabled="showLoading">
            {{ status === 'loadmore' ? '点击加载更多' : status === 'loading' ? '正在加载' : '到底啦~' }}
          </TnButton>
        </view>
      </view>
      <view class="back" :style="{display: scrollTop > 500 ? 'flex' : 'none'}" @click.stop="scrollToTop">
        <TnIcon name="totop-simple" style="font-size: 25px"/>
      </view>
    </scroll-view>
    <view class="cu-modal" :class=" showInstruction ? 'show' : ''" @click="showInstruction = false">
      <view
          class="cu-dialog tn-text-left article-shadow padding"
          @click.stop=""
          style="background-color: #fff;color: #666666;">
        <view class="flex justify-between">
          <view style="width: 5%">
            ①
          </view>
          <view style="width: 93%;line-height: 1.5">
            公众号会不定时更新内容(包括但不限于攻略、配置分享、每周更新、下周预告等)
          </view>
        </view>
        <view class="flex justify-between">
          <view style="width: 5%">
            ②
          </view>
          <view style="width: 93%;line-height: 1.5">
            由于本人比较蠢笨, 上述内容不出意外大概率是从其他博主和官网上搬运, 文中会注明出处, 还望各位大佬理解!(*╹▽╹*)
          </view>
        </view>
        <view class="margin-top">长按下方二维码关注公众号可查看更多内容~ 其实也没有什么内容啦~(#^.^#)</view>
        <image show-menu-by-longpress style="width: 100%" mode="widthFix"
               src="https://seerforwdw.info/seer-image/weixin_public.jpg"></image>
      </view>
    </view>
    <loading-page :show="showLoading" @click.stop=""/>
  </view>
</template>

<script setup>
  import {useUniAppSystemRectInfo} from "../../uni_modules/tuniaoui-vue3/hooks/use-uniapp-system-rect-info";
  import {ref, onMounted, getCurrentInstance, computed, reactive, nextTick} from 'vue'
  import TnNavbar from "../../uni_modules/tuniaoui-vue3/components/navbar/src/navbar.vue";
  import LoadingPage from "../../components/loadingPage.vue";
  import TnButton from "../../uni_modules/tuniaoui-vue3/components/button/src/button.vue";
  import TnIcon from "@/uni_modules/tuniaoui-vue3/components/icon/src/icon.vue";

  const showLoading = ref(false)

  const {height} = useUniAppSystemRectInfo().navBarInfo
  const safeBottom = ref(0)

  const {
    proxy
  } = getCurrentInstance()

  const showInstruction = ref(false)

  const list = ref([])
  // 分页数据
  const where = reactive({
    page: 1,
    content: '',
    limit: 20,
    def_name: '',
    sort: 'id',
    order: 'asc'
  })
  const totalPages = ref(0)

  const getList = async () => {
    showLoading.value = true;
    if (totalPages.value === 1) {
      ({records: list.value, total_count: totalPages.value} = await proxy.$api.getPublicAccountArticleQuery(where))
    } else {
      const {records} = await proxy.$api.getPublicAccountArticleQuery()
      list.value = [...list.value, ...records]
    }

    list.value.forEach(item => {
      item.content = JSON.parse(item.content)
    })
    showLoading.value = false;
  }

  const status = computed(() => {
    if (showLoading.value) {
      return 'loading'
    } else if (where.page < totalPages.value) {
      return 'loadmore'
    } else {
      return 'nomore'
    }
  })

  // 触底触发
  const loadMore = async () => {
    if (status.value === 'loading') return
    if (status.value === 'loadmore') {
      where.page++
      await getList()
    } else {
      showLoading.value = false
    }
  }

  // 滚动触发
  const scrollTop = ref(0)
  const topItem = ref('')
  const handleScroll = (ev) => {
    scrollTop.value = ev.detail.scrollTop
    topItem.value = ''
  }

  // 返回顶部
  const scrollToTop = () => {
    nextTick(() => {
      topItem.value = 'top'
      scrollTop.value = 0
    })
  }

  // 进入详情页
  const gotoArticleDetail = (url) => {
    const data = {url}
    uni.navigateTo({
      url: '/pages/update-article/article-content?data=' + encodeURIComponent(JSON.stringify(data))
    })
  }

  onMounted(async () => {
    uni.getSystemInfo({
      success: res => {
        safeBottom.value = res.safeAreaInsets.bottom
      }
    });
    await getList()
  })
</script>


<style scoped lang="scss">
  /* 资讯主图 start*/
  .image-article {
    border-radius: 8rpx;
    border: 1rpx solid #F8F7F8;
    width: 250rpx;
    height: 200rpx;
    position: relative;
  }

  .image-pic {
    background-size: cover;
    background-repeat: no-repeat;
    // background-attachment:fixed;
    background-position: top;
    border-radius: 10rpx;
  }

  .article-shadow {
    border-radius: 15rpx;
    box-shadow: 0rpx 0rpx 50rpx 0rpx rgba(0, 0, 0, 0.07);
  }

  /* 文字截取*/
  .clamp-text-1 {
    -webkit-line-clamp: 1;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .clamp-text-2 {
    -webkit-line-clamp: 2;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  /* 标签内容 start*/
  .tn-tag-content {
    &__item {
      display: inline-block;
      line-height: 35rpx;
      padding: 5rpx 25rpx;

      &--prefix {
        padding-right: 10rpx;
      }
    }
  }

  .tn-text-justify {
    text-align: justify;
  }

  /* 标签内容 end*/

  .back {
    width: 50px;
    height: 50px;
    border-radius: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgba(0, 0, 0, .4);
    color: #fff;
    position: fixed;
    right: 5px;
    bottom: 120px;
    z-index: 10001;
  }
</style>
